package org.vaadinpa.gui.views;

import org.vaadin.thomas.timefield.TimeField;
import org.vaadinpa.Conf;
import org.vaadinpa.data.Planet;
import org.vaadinpa.data.Resource;
import org.vaadinpa.data.Universe;
import org.vaadinpa.data.serializer.PASerializer;
import org.vaadinpa.data.ships.Agamemnon;
import org.vaadinpa.data.ships.Helen;
import org.vaadinpa.data.ships.Menelaus;
import org.vaadinpa.data.ships.Paris;
import org.vaadinpa.gui.AbstractView;
import org.vaadinpa.gui.VaadinPA;
import org.vaadinpa.logic.Ticker;

import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.DateField;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;

@SuppressWarnings("serial")
public class AdminView extends AbstractView {

	private Label tickLabel;

	public AdminView() {

		final TextField pwField = new TextField("Admin password");
		pwField.setValue("");
		pwField.focus();
		addComponent(pwField);

		Button login = new Button("login");
		login.setClickShortcut(KeyCode.ENTER);
		addComponent(login);

		login.addListener(new ClickListener() {

			@Override
			public void buttonClick(ClickEvent event) {
				if (pwField.getValue().equals(Conf.ADMIN_PASSWORD)) {
					changeToReal();
				}
			}
		});
	}

	public void changeToReal() {
		removeAllComponents();
		Button begin = new Button("start");
		begin.addListener(new ClickListener() {

			public void buttonClick(ClickEvent event) {
				Ticker.startTicks();
			}
		});
		addComponent(begin);

		Button end = new Button("end");
		end.addListener(new ClickListener() {

			public void buttonClick(ClickEvent event) {
				Ticker.endTicks();
			}
		});
		addComponent(end);

		final TimeField tf = new TimeField();
		tf.setHours(0);
		tf.setResolution(DateField.RESOLUTION_SEC);
		addComponent(tf);
		tf.set24HourClock(true);
		Button delayedStart = new Button("Delayed Start");
		delayedStart.addListener(new ClickListener() {

			public void buttonClick(ClickEvent event) {
				long time = 0L;
				time += tf.getSeconds();
				time += tf.getMinutes() * 60;
				time += tf.getHours() * 60 * 60;
				Ticker.startTicksAfter(time * 1000);
			}
		});
		addComponent(delayedStart);

		Button parse = new Button("Parse");
		parse.addListener(new ClickListener() {

			public void buttonClick(ClickEvent event) {
				try {
					PASerializer.parse();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		addComponent(parse);

		tickLabel = new Label();
		addComponent(tickLabel);

		Label coordsLabel = new Label(VaadinPA.getPlanet().coords.x + " " + VaadinPA.getPlanet().coords.y);
		addComponent(coordsLabel);

		Button newRoidsButton = new Button("addfleets", new ClickListener() {

			public void buttonClick(ClickEvent event) {

				boolean b = true;
				for (Planet p : Universe.instance.values()) {

					if (b) {
						p.fleets.get(0).addShips(new Menelaus(), 30);
						p.fleets.get(0).addShips(new Agamemnon(), 30);
						p.fleets.get(0).addShips(new Helen(), 30);
						b = false;
					} else {
						p.fleets.get(0).addShips(new Agamemnon(), 50);
						p.fleets.get(0).addShips(new Paris(), 150);
					}

					for (Resource r : Resource.values()) {
						p.roids.put(r, 50);
					}
				}

			}
		});
		addComponent(newRoidsButton);

		Button addMoney = new Button("Add monies");
		addMoney.addListener(new ClickListener() {

			public void buttonClick(ClickEvent event) {
				VaadinPA.getPlanet().depositFunds(1000000, 1000000, 100000);
			}
		});
		addComponent(addMoney);

	}

	@Override
	public void reload() {
	}

	@Override
	public String getTitle() {
		return "Admin view";
	}

}
